// pages/goods/detail/detail.js
var api = require('../../../utils/api.js')
var app = new getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navH: 0,
    isIphoneX: false,
    api:api,
    type:0,
    content:[],
    current:0,
    path:api.path,
    Hei:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //options.scene = "%3Fid%3D515%26type%3D1"
    let id = ""
    console.log("detail.js")
    console.log(options)
    if (options.scene) {
      //获取二维码的携带的链接信息
      let qrUrl = decodeURIComponent(options.scene)
      console.log(qrUrl)
      //获取链接中的参数信息
      id = this.getQueryString(qrUrl, 'id')
      options.type = this.getQueryString(qrUrl, 'type')
    }else{
      id =options.id
    }
    let type =options.type
    let src = ''
    if(type==1){
      this.setData({
        type:type
      })
      src = 'soft_goods'
    }else{
      src = 'goods'
    }
    console.log("id"+id)
    let params = {
      id:id,
      member_id:api.cache('user').member_id
    }
    api.post(src,params).then(res=>{
      if(res.code==200){
        this.setData({
          content:res.data,
        })
      }
    })
    this.getCountHeight()
  },
  getCountHeight(){
    let isIphoneX = app.globalData.isIphoneX
    if (isIphoneX) {
      this.setData({
        isIphoneX
      })
    }
    this.setData({
      navH: app.globalData.navHeight
    })
  },
  getQueryString(url, name) {
    console.log("url = " + url)
    console.log("id = " + name)
    var reg = new RegExp('(^|&|/?)' + name + '=([^&|/?]*)(&|/?|$)', 'i')
    var r = url.substr(1).match(reg)
    if (r != null) {
      console.log("r = " + r)
      console.log("r[2] = " + r[2])
      return r[2]
    }
    return null;
  },
  changeBig(e){
    let index = e.currentTarget.dataset.current;
    let data = this.data.content.goods.pics.map(ele=>api.path+ele.thumb)
    let url = data[index]
    wx.previewImage({
      current:url,
      urls:data
    })
  },
  getImgPath(str) {
    // 匹配图片（g表示匹配所有结果i表示区分大小写）
    const imgReg = /<img.*?(?:>|\/>)/gi
    // 匹配src属性
    const srcReg = /src=[\'\"]?([^\'\"]*)[\'\"]?/i
    
    const arr = str.match(imgReg)
    let imgs = []
    for (let i = 0; i < arr.length; i++) {
      const src = arr[i].match(srcReg)
      // 获取图片地址
      if (src[1]){
        imgs.push(src[1])
      }
    }
    return imgs;
  },
  preview(){
    let text = this.data.content.goods.content
    let urls = this.getImgPath(text)
    console.log(urls)
    wx.previewImage({
      current:urls[0],
      urls:urls
    })
  },
  operation(e){
    let user = api.cache("user")
    let res = api.checkLogin()
    if (!res) {
      return;
    }
    let index = e.currentTarget.dataset.index
    let url = ''
    if(index==1){
      if(this.data.content.is_fav==0){
        url = 'click_fav'
      }else{
        url = 'del_fav_single'
      }
    }else if(index==2){
      if(this.data.content.is_love==0){
        url = 'click_zan'
      }else{
        url = 'del_love_single'
      }
    }else if(index==3){
      url = 'click_share'
    }
    let params={
      id:this.data.content.goods.id,
      data_type:1,
      member_id:user.member_id,
    }
    api.post(url,params).then(res=>{
      if(res.code==200){
        let content = this.data.content
        if(index==1){
          if(this.data.content.is_fav==0){
            content.fav_total +=1
            content.is_fav = 1
            api.tip("成功收藏")
          }else{
            content.fav_total -=1
            content.is_fav = 0
            api.tip("取消收藏")
          }
        }else if(index==2){
          if(this.data.content.is_love==0){
            content.love_total +=1
            content.is_love = 1
            api.tip("成功点赞")
          }else{
            content.love_total -=1
            content.is_love = 0
            api.tip("取消点赞")
          }
        }else if(index==3){
          content.share_total +=1
        }
        this.setData({
          content
        })
      }
    })
  },
})